package com.hnkywl.sts.service.cwgl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.cwgl.KhzhsfmxDao;
import com.hnkywl.sts.dao.cwgl.KmdmDao;
import com.hnkywl.sts.entity.cwgl.Khzhsfmx;
import com.hnkywl.sts.entity.cwgl.Kmdm;
import com.hnkywl.sts.entity.htgl.Ht;
import com.hnkywl.sts.entity.jhgl.Jkd;
import com.hnkywl.sts.entity.organ.Nbbm;
import com.hnkywl.sts.entity.organ.Nbgs;
import com.hnkywl.sts.entity.sfgl.Sfk;
import com.hnkywl.sts.entity.sfgl.Sfkmx;
import com.hnkywl.sts.entity.xsgl.Kcbj;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.orm.hibernate.OrderBy;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-3-25
 * Time: 下午3:38
 * To change this template use File | Settings | File Templates.
 */
@Service
public class KmdmService {
    private KmdmDao kmdmDao;

    /**
     * 保存科目代码
     *
     * @param kmdm 科目代码实体
     * @return
     */
    @Transactional
    public void saveKmdm(Kmdm kmdm) {
        this.kmdmDao.save(kmdm);
    }

    /**
     * 生成上级科目代码
     *
     * @param gsId 科目代码实体
     * @return
     */
    @Transactional
    public void createSjcode(Long gsId) {
        this.kmdmDao.createSjcode(gsId);
    }

    @Transactional
    public void createSfxj(Long gsId) {
        this.kmdmDao.createSfxj(gsId);
    }

    @Transactional
      public void createKmdl(Long gsId) {
        this.kmdmDao.createKmdl(gsId);
    }

    @Transactional
    public void updateFzhslx(Long gsId) {
        this.kmdmDao.updateFzhslx(gsId);
    }



    /**
     * 删除科目代码通过公司
     *
     * @param gsId 公司Id
     * @return
     */
    @Transactional
    public void deleteKmdmByGs(Long gsId) {
        List<Kmdm> kmdmList = this.kmdmDao.findKmdmByGs(gsId);
        for (Kmdm kmdm : kmdmList) {
            this.kmdmDao.delete(kmdm);
        }
    }

    public String getKmbmTreeJson(String sjcode, String kmdl, Long gsId) {
        return kmdmDao.getKmbmTreeJson(sjcode, kmdl, gsId);
    }


    @Transactional
    public Kmdm findKmdmDetail(Long id) {
        return this.kmdmDao.get(id);
    }

    @Transactional
    public Kmdm findKmdmDetailByGs(Long gsId, String codeid) {
        return this.kmdmDao.findKmdmDetailByGs(gsId, codeid);
    }

    /**
     * 查询库存报价列表
     *
     * @param gsId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Transactional
    public String findAllKmdm(Long gsId, String name, String code, int pageNo, int pageSize) {
        Page<Map> page = this.kmdmDao.findAllKmdm(gsId, name, code, pageNo, pageSize);
        List<Map> list = page.getResult() == null ? new ArrayList<Map>() : page.getResult();
        GsonBuilder gb = new GsonBuilder();
        String json = gb.create().toJson(list);
        return "{\"total\":" + page.getTotalCount() + ",\"rows\":" + json + "}";
    }


    public String findKmdm(Long gsId, String code, int pageSize) {
        Page<Kmdm> page = kmdmDao.findKmdm(gsId, code, pageSize);
        String json = BaseUtils.fetchCollectionToJson(page.getResult(), "id", "code", "name");
        return "[" + json + "]";
    }

    public String findKmdmXm(Long gsId, int pageNo, int pageSize) {
        Page<Map> page = kmdmDao.findKmdmXm(gsId,1,-1);
        String json = BaseUtils.fetchCollectionToJson(page.getResult(), "id", "code", "name","fzhslx");
        return "[" + json + "]";
    }

    /**
     * 更新科目代码
     *
     * @param kmdm 付款审核实体
     * @return
     */
    @Transactional
    public void updateKmdm(Kmdm kmdm) {
        Kmdm clone = this.findKmdmDetail(kmdm.getId());
        clone = (Kmdm) ReflectionUtils.getInstance(clone, kmdm);
        clone.setLastUpdated(new Date());
        this.kmdmDao.save(clone);
    }

    /**
     * 查询科目名称路径
     *
     * @param gsid
     * @param code
     * @return
     */
    @Transactional
    public String findKmPathName(Long gsid, String code) {
        return kmdmDao.findKmPathName(gsid, code);
    }

    @Autowired
    public void setKmdmDao(KmdmDao kmdmDao) {
        this.kmdmDao = kmdmDao;
    }
}
